package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.AddressBook;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.service.IAddressBookService;
import com.itheima.util.Result;
import com.itheima.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 老师好我是郁同学
 * @date 2022/10/11
 **/
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public Result saveAddressBooks(AddressBook addressBook) {
        //获取当前用户的地址
        Long userId = UserHolder.getUserId();
        //设置用户的id
        addressBook.setUserId(userId);
        //查找是否有该用户的地址
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,userId);
        List<AddressBook> addressBooks = addressBookMapper.selectList(lqw);
        //判断该用户是否有地址信息
        if (addressBooks.isEmpty()) {
            //地址信息不为空，不是第一次添加信息，保存地址信息
            int r = addressBookMapper.insert(addressBook);
            if(r<=0){
                return Result.error("添加失败");
            }
            return Result.success(addressBook);
        }
        //第一次添加
        //需要将该次地址设置为默认地址
        addressBook.setIsDefault(1);
        int r = addressBookMapper.insert(addressBook);
        //判断是否添加成功
        if(r<=0){
            return Result.error("添加失败");
        }
        return Result.success(addressBook);
    }

    /**
     * 展示默认地址
     * @return
     */
    @Override
    public Result defaultAddressBooks() {
        //默认地址的标识是 1
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,UserHolder.getUserId())
                .eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookMapper.selectOne(lqw);
        //判断默认地址是否存在
        if (addressBook == null) {
            return Result.error("没有默认地址");
        }
        return Result.success(addressBook);
    }

    /**
     * 展示地址信息列表
     * @return
     */
    @Override
    public Result addressBooksList() {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //根据当前用户id查询当前用户所属的所有的地址信息
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,userId);
        List<AddressBook> addressBooks = addressBookMapper.selectList(lqw);
        //判断是否存在地址信息
        if (addressBooks.isEmpty()) {
            return Result.error("没有设置地址");
        }
        return Result.success(addressBooks);
    }

    @Override
    public Result putAddressBooks(AddressBook addressBook) {
        //获取当前的用户的id
        Long userId = UserHolder.getUserId();
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        //查找原先的默认地址
        lqw.eq(AddressBook::getUserId,userId)
                .eq(AddressBook::getIsDefault,1);
        AddressBook addressBook1 = addressBookMapper.selectOne(lqw);
        addressBook1.setIsDefault(0);
        //将原来的默认地址的标识更改为0
        addressBookMapper.updateById(addressBook1);
        //根据id查询的地址
        AddressBook selectOne = addressBookMapper.selectById(addressBook.getId());
        //将当前的地址设置为默认地址
        selectOne.setIsDefault(1);
        addressBookMapper.updateById(selectOne);
        return Result.success(selectOne);
    }
}
